package m.jclecai.com.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import m.jclecai.com.R;
import m.jclecai.com.activity.follow.HotOrderDetailActivity;
import m.jclecai.com.bean.Response.ResHotPeopleInfo;
import m.jclecai.com.bean.bifen.MatchTeam;

/**
 * Created by  HONGDA on 2018/6/19.
 */
public class BiFenChildChildAdapter extends RecyclerView.Adapter<BiFenChildChildAdapter.HotPeopleViewHolder> {

    private Activity context;
    public JSONObject mData ;
    private LayoutInflater inflater;
    private JSONArray matches;
    private JSONObject events;

    public BiFenChildChildAdapter(Activity context, JSONObject mData, JSONArray matches,JSONObject events) {
        this.context = context;
        this.mData = mData;
        this.matches=matches;
        this.events=events;

        inflater = LayoutInflater.from(context);
    }

    @Override
    public BiFenChildChildAdapter.HotPeopleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_bifen_child_child, parent, false);
        HotPeopleViewHolder holder = new HotPeopleViewHolder(view);
        return holder;
    }

    public void refresh(JSONObject mData,JSONArray matches) {
//        if (mData != null) {
//            mData
//            addData(datas,matches);
//        }
    }

    public void addData(JSONObject mData,JSONArray matches,JSONObject events) {
//        mData.addAll(datas);
        this.matches=matches;
        this.mData=mData;
        this.events=events;

        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(HotPeopleViewHolder holder , int position) {
//        if (mData != null) {
//            ResHotPeopleInfo.HotPeopleOrderBean str = mData.get(position);
//            ((HotPeopleViewHolder) holder).init(str);
//        }
//        holder.textView.setText(str);
        if (matches!=null){

            try {
                  JSONArray jsonArray = matches.getJSONArray(position);


                  //联赛(通过联赛id 获取events（赛事）)
                  JSONObject event = events.getJSONObject(jsonArray.getInt(1) + "");
                  holder.item_child_child_tv_game_dengji.setText(event.getString("name_zh"));


                  //状态id 0比赛异常 1未开赛 2上半场 3中场 4下半场 5加时赛上半场 6加时赛下半场 7点球决战 8完场 9推迟 10中断 11腰斩 12取消 13待定
                  if (jsonArray.getInt(2)==8){
                        holder.totalScore.setVisibility(View.VISIBLE);
                        holder.matchState.setText("完赛");
                  }else {
                        holder.totalScore.setVisibility(View.GONE);
                  }

                  //主队
                  JSONArray homeTeam = jsonArray.getJSONArray(5);
                  Log.d("======",homeTeam.toString());

                  JSONObject home = mData.getJSONObject(homeTeam.getInt(0) + "");//通过id 查询球队
                  Glide.with(context).load("https://cdn.leisu.com/teamflag_s/"+home.getString("logo")).into(holder.homeImg);
                  holder.homeTeam.setText(home.getString("name_zh"));

                  //客队
                  JSONArray visitingTeam = jsonArray.getJSONArray(6);
                  JSONObject vice = mData.getJSONObject(visitingTeam.getInt(0) + "");//通过id 查询球队
                  Glide.with(context).load("https://cdn.leisu.com/teamflag_s/"+vice.getString("logo")).into(holder.viceImg);

                  //比赛详情
                  JSONArray matchesDetail = jsonArray.getJSONArray(7);
                  if (matchesDetail.getString(3).contains("_")){
                      String[] split = matchesDetail.getString(3).split("_");
                      holder.matchNum.setText(split[1]);
                  }else {
                      holder.matchNum.setText(matchesDetail.getString(3));
                  }

                  //赛季信息
                  JSONArray competitionSeason = jsonArray.getJSONArray(8);


                  holder.totalScore.setText(homeTeam.getInt(2)+":"+visitingTeam.getInt(2));
                  holder.halfScore.setText("半场"+homeTeam.getInt(3)+":"+visitingTeam.getInt(3));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public int getItemCount() {
        return matches == null ? 0 : matches.length();
    }

    public class HotPeopleViewHolder extends RecyclerView.ViewHolder {

        TextView item_child_child_tv_game_dengji,//比赛名称 如世界杯
                matchNum,// 比赛场次 或时间  如 周三 061
                homeTeam,// 主场队伍

                viceTeam,// 客场队伍

                matchState,//开赛状态（未开赛）
                totalScore,//总比分
                halfScore; //半决赛比分
        ImageView homeImg,//主场队伍图标
                  viceImg;//客场队伍图标
        View view;

        public HotPeopleViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            item_child_child_tv_game_dengji=itemView.findViewById(R.id.item_child_child_tv_game_dengji);
            matchNum=itemView.findViewById(R.id.matchNum);
            homeTeam=itemView.findViewById(R.id.homeTeam);
            homeImg=itemView.findViewById(R.id.homeImg);
            viceTeam=itemView.findViewById(R.id.viceTeam);
            viceImg=itemView.findViewById(R.id.viceImg);
            matchState=itemView.findViewById(R.id.matchState);
            totalScore=itemView.findViewById(R.id.totalScore);
            halfScore=itemView.findViewById(R.id.halfScore);

        }

        public void init(final ResHotPeopleInfo.HotPeopleOrderBean hotPeopleOrder) {
//            mTvGroupBuyTitle.setText(hotPeopleOrder.getLotteryname());
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = HotOrderDetailActivity.getIntent(context, hotPeopleOrder.getId());
                    context.startActivity(intent);
                }
            });

        }
    }

}
